package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.sc.nft.dao.EcologyHatchExchangeConfDao;
import com.sc.nft.dao.EcologyHatchUsePropsExchangeDao;
import com.sc.nft.dao.EquityPropsDao;
import com.sc.nft.dao.UserEquityPropsDao;
import com.sc.nft.entity.*;
import com.sc.nft.entity.req.EcologyHatchUsePropsExchangeRequest;
import com.sc.nft.entity.vo.ecology.EcologyHatchExchangeConfPropDetailsVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.EcologyHatchUsePropsExchangeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author 朱梦晨
 * @Description:
 * @date 2024-08-06
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class EcologyHatchUsePropsExchangeServiceImpl implements EcologyHatchUsePropsExchangeService {



    private final EcologyHatchUsePropsExchangeDao ecologyHatchUsePropsExchangeDao;

    private final EquityPropsDao equityPropsDao;

    private final UserEquityPropsDao userEquityPropsDao;


    /**
     * 编辑熔炼上限道具关联表
     * @param ecologyHatchId
     * @param confId
     * @param ecologyHatchUsePropsExchangeRequestList
     */
    @Override
    public void editEcologyHatchUsePropsExchange(Long ecologyHatchId, Long confId,
                                                 List<EcologyHatchUsePropsExchangeRequest> ecologyHatchUsePropsExchangeRequestList) {
        // 熔炼上限配置关联道具表列表
        List<EcologyHatchUsePropsExchange> ecologyHatchUsePropsExchangeList = findEcologyHatchUsePropsExchangeListByConfId(confId);
        List<Long> idsInDb = ecologyHatchUsePropsExchangeList.stream().map(EcologyHatchUsePropsExchange::getId).collect(Collectors.toList());
        List<Boolean> statusList = ecologyHatchUsePropsExchangeRequestList.stream().map(EcologyHatchUsePropsExchangeRequest::getStatus).collect(Collectors.toList());
        if (!statusList.contains(Boolean.TRUE)){
            throw new GlobalRunTimeException("请至少开启一条兑换道具卡");
        }
        for (EcologyHatchUsePropsExchangeRequest req : ecologyHatchUsePropsExchangeRequestList) {
            //道具
            EquityProps equityProps = equityPropsDao.getById(req.getPropId());
            Assert.notNull(equityProps, () -> new GlobalRunTimeException("道具卡不存在"));
            Long id = req.getId();
            if (Objects.isNull(id)){

                EcologyHatchUsePropsExchange ecologyHatchUsePropsExchange = BeanUtil.copyProperties(req, EcologyHatchUsePropsExchange.class);
                ecologyHatchUsePropsExchange.setEcologyHatchExchangeConfId(confId);
                ecologyHatchUsePropsExchange.insert();
            }else {
                idsInDb.remove(id);
                EcologyHatchUsePropsExchange ecologyHatchUsePropsExchange = ecologyHatchUsePropsExchangeDao.getOneByEcologyHatchId(ecologyHatchId, confId);
                Assert.notNull(ecologyHatchUsePropsExchange, () -> new GlobalRunTimeException("请至少开启一条兑换道具卡"));
                // 修改
                Assert.isTrue(ecologyHatchUsePropsExchangeDao.updateEcologyHatchUsePropsById(id,req.getPropId(), req.getPropCount(),
                        req.getPropImage(),req.getStatus()), () -> new GlobalRunTimeException("生态熔炼上限配置更新失败"));
            }
        }
    }

    /**
     * 获取熔炼上限兑换配置道具卡详情
     * @param confId
     * @return
     */
    @Override
    public List<EcologyHatchUsePropsExchange> findAdminEcologyHatchUsePropsExchangeListByConfId(Long confId) {
        return ecologyHatchUsePropsExchangeDao.findAdminEcologyHatchUsePropsExchangeListByConfId(confId);
    }


    /**
     * 获取熔炼上限兑换配置道具卡详情
     * @param confId
     * @return
     */
    @Override
    public List<EcologyHatchUsePropsExchange> findEcologyHatchUsePropsExchangeListByConfId(Long confId) {
        return ecologyHatchUsePropsExchangeDao.findEcologyHatchUsePropsExchangeListByConfId(confId);
    }

    /**
     * 获取生态熔炼上限兑换配置消耗道具卡详情 带拥有数量
     * @param confId
     * @param userId
     * @return
     */
    @Override
    public List<EcologyHatchExchangeConfPropDetailsVO> findEcologyHatchExchangeConfPropList(Long confId,Long userId){

        List<EcologyHatchUsePropsExchange> ecologyHatchUsePropsExchangeList = findEcologyHatchUsePropsExchangeListByConfId(confId);
        List<EcologyHatchExchangeConfPropDetailsVO> ecologyHatchExchangeConfPropDetailsVOS = BeanUtil.copyToList(ecologyHatchUsePropsExchangeList, EcologyHatchExchangeConfPropDetailsVO.class);
        for (EcologyHatchExchangeConfPropDetailsVO ecologyHatchExchangeConfPropDetailsVO : ecologyHatchExchangeConfPropDetailsVOS) {
            //道具卡拥有数量
            Integer havingCountByUserId = userEquityPropsDao.getHavingCountByUserId(ecologyHatchExchangeConfPropDetailsVO.getPropId(), userId);
            ecologyHatchExchangeConfPropDetailsVO.setHasCount(havingCountByUserId);
        }
        return ecologyHatchExchangeConfPropDetailsVOS;
    }


    /**
     * 更新兑换熔炼关联道具卡状态
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, Boolean status) {

        EcologyHatchUsePropsExchange ecologyHatchUsePropsExchange = ecologyHatchUsePropsExchangeDao.getById(id);
        Assert.notNull(ecologyHatchUsePropsExchange, () -> new GlobalRunTimeException("兑换配置关联道具卡不存在"));
        Assert.isFalse(ecologyHatchUsePropsExchange.getStatus().equals(status), () -> new GlobalRunTimeException("状态未发生改变"));
        Assert.isTrue(ecologyHatchUsePropsExchangeDao.updateStatusById(id, status), () -> new GlobalRunTimeException("更新失败"));
    }

    @Override
    public void dealExchangeUsePropsById(Long id) {
        EcologyHatchUsePropsExchange ecologyHatchUsePropsExchange = ecologyHatchUsePropsExchangeDao.getById(id);
        Assert.notNull(ecologyHatchUsePropsExchange, () -> new GlobalRunTimeException("兑换配置关联道具卡不存在"));

        Assert.isTrue(ecologyHatchUsePropsExchangeDao.dealExchangeUsePropsById(id), () -> new GlobalRunTimeException("删除失败"));
    }


}
